package org.siqisource.webstart.tools.proxy;

import java.io.File;
import java.io.IOException;

import org.zeroturnaround.zip.NameMapper;
import org.zeroturnaround.zip.ZipUtil;

public class AppUtil {
	public static void extractApache() {
		try {
			// 删除目标文件夹
			delDir(ApacheStart.APACHE_HOME);
			// 解压缩到目标文件夹
			String jarFileName = getJarPath();
			ZipUtil.unpack(new File(jarFileName), new File(ApacheStart.APACHE_HOME), new NameMapper() {

				public String map(String name) {
					return name.startsWith(ApacheStart.APACHE_FOLDER) ? name.replace(ApacheStart.APACHE_FOLDER, "") : null;
				}
			});
		} catch (IOException e) {
			
		}

	}
	
	public static void delDir(String filepath) throws IOException{
		File f = new File(filepath);//定义文件路径   
		if(f.exists() && f.isDirectory()){
			if(f.listFiles().length==0){//若目录下没有文件则直接删除
				f.delete();
			}else{
				File delFile[]=f.listFiles();
				int i =f.listFiles().length;
				for(int j=0;j<i;j++){
	        		if(delFile[j].isDirectory()){
	        			delDir(delFile[j].getAbsolutePath());//递归调用del方法并取得子目录路径
	        	
	        		}
	        		delFile[j].delete();//删除文件
	        	}
			}
		}
	}
	
	public static String getJarPath() {
		java.net.URL url = App.class.getProtectionDomain().getCodeSource().getLocation();
		String filePath = "/";
		try {
		filePath = java.net.URLDecoder.decode(url.getPath(), "utf-8");
		} catch (Exception e) {
		e.printStackTrace();
		}
		return filePath.substring(1);
	}
}
